<?php

namespace haibao\lightgoods\business\misaki;

use \haibao\erp\model\filter\product\ProductContentInfo as ProductContentInfoFilter;
use \haibao\erp\model\data\product\ProductContentInfo as ProductContentInfoData;
use \haibao\frame\data\query\Condition;
use \haibao\frame\data\query\Pager;
use \haibao\lightgoods\common\Misaki as MisakiCommon;
use \haibao\erp\business\product\Product as ProductBusiness;
use \haibao\erp\model\filter\product\Product as ProductFilter;
use \haibao\erp\business\product\ProductCategory as ProductCategoryBusiness;

class Misaki extends \haibao\erp\business\Base{
	
	/**
	 * 通过分类ID得到misaki_product
	 * @param  $categoryId
	 */
    public static function getProductByCategoryId($categoryId, $page=1){
        $productFilter = new ProductFilter();

        if ($categoryId){
            if(is_array($categoryId)){
                $productFilter->where(ProductFilter::CONDITION_FIELD_NAME_CATEGORY_ID,  Condition::CONDITION_IN,$categoryId);
            }else{
                $productFilter->where(ProductFilter::CONDITION_FIELD_NAME_CATEGORY_ID,  Condition::CONDITION_EQUAL,$categoryId);
            }
        }
        $productFilter->where(ProductFilter::CONDITION_FIELD_NAME_PRODUCT_TYPE,  Condition::CONDITION_EQUAL,MisakiCommon::PRODUCT_DATATYPE);
        $productFilter->where(ProductFilter::CONDITION_FIELD_NAME_STATUS,  Condition::CONDITION_NOTEQUAL, \haibao\erp\model\data\product\Product::STATUS_DELETE);
        $productFilter->select(array(
            ProductFilter::CONDITION_FIELD_NAME_ID,
            ProductFilter::CONDITION_FIELD_NAME_PRODUCT_NAME,
            ProductFilter::CONDITION_FIELD_NAME_CATEGORY_ID,
            ProductFilter::CONDITION_FIELD_NAME_TITLE,
        ));
        $productContenInfoFilter = new ProductContentInfoFilter();
        $productContenInfoFilter->select(array(
            $productContenInfoFilter::CONDITION_FIELD_NAME_PRODUCT_ID,
            $productContenInfoFilter::CONDITION_FIELD_NAME_TOTAL_DATATYPE,
            $productContenInfoFilter::CONDITION_FIELD_NAME_STATUS,
            $productContenInfoFilter::CONDITION_FIELD_NAME_CONTENTINFO,
        ));
        $productContenInfoFilter->where($productContenInfoFilter::CONDITION_FIELD_NAME_TOTAL_DATATYPE,Condition::CONDITION_IN,array(\haibao\erp\model\data\product\ProductContentInfo::DATA_TYPE_IMAGE_PRO,\haibao\erp\model\data\product\ProductContentInfo::DATA_TYPE_PRICE));

        $productFilter->leftJoin($productContenInfoFilter,null,ProductFilter::CONDITION_FIELD_NAME_ID,$productContenInfoFilter::CONDITION_FIELD_NAME_PRODUCT_ID,ProductFilter::CONDITION_FIELD_NAME_PRODUCT_CONTENT_INFO);
        $productFilter->order(ProductFilter::CONDITION_FIELD_NAME_ID,false);
        $productFilter->setPager(new Pager(4,$page));
        $productList = ProductBusiness::getProduct($productFilter);


        $imageIdArr = array();
        foreach ($productList as $key => $value){
            foreach($value->ProductContentInfo as $info){
                if($info->DataType == ProductContentInfoData::DATA_TYPE_PRICE){
                    $price = json_decode($info->ContentInfo,true);
                    $value->ProductPrice = $price[MisakiCommon::MISAKI_PRODUCT_PRICE_ACCURY];
                }elseif($info->DataType == ProductContentInfoData::DATA_TYPE_IMAGE_PRO){
                    array_push($imageIdArr, $info->ContentInfo);
                    $value->ProductListImage = $info->ContentInfo;
                    break;
                }
            }
        }
        $imageList = ProductBusiness::getInfobyImgeIdArr($imageIdArr);
       // print_r($productList);exit;
        foreach ($productList as $k=>$product){
            if(isset($imageList[$product->ProductListImage])){
                $image = $imageList[$product->ProductListImage];
                $product->ProductListImage = \haibao\cms\business\control\Base::getImageLink(substr($image, 1,strlen($image)), 0, 0, 90, 0);
                $product->ProductListImage = str_replace('http://static.localhaibao.cn:81', 'http://static.haibao.cn', $product->ProductListImage);
            }

        }
        return array($productList, $productFilter->getPager());
    }




	public static function getproductbyDatatype($productType,$pagesize=100,$productId=false){

        $productFilter = new ProductFilter();
        if($productType){
            $productFilter->where(ProductFilter::CONDITION_FIELD_NAME_CATEGORY_ID,  Condition::CONDITION_EQUAL,$productType);
        }
        if($productId != false){
            $productFilter->where(ProductFilter::CONDITION_FIELD_NAME_ID,  Condition::CONDITION_NOTEQUAL,$productId);

        }
        $productFilter->where(ProductFilter::CONDITION_FIELD_NAME_STATUS,  Condition::CONDITION_NOTEQUAL, \haibao\erp\model\data\product\Product::STATUS_DELETE);
        $productFilter->select(array(
            ProductFilter::CONDITION_FIELD_NAME_ID,
            ProductFilter::CONDITION_FIELD_NAME_PRODUCT_NAME,
            ProductFilter::CONDITION_FIELD_NAME_CATEGORY_ID,
            ProductFilter::CONDITION_FIELD_NAME_TITLE,
        ));
        $productContenInfoFilter = new ProductContentInfoFilter();
        $productContenInfoFilter->select(array(
            $productContenInfoFilter::CONDITION_FIELD_NAME_PRODUCT_ID,
            $productContenInfoFilter::CONDITION_FIELD_NAME_TOTAL_DATATYPE,
            $productContenInfoFilter::CONDITION_FIELD_NAME_STATUS,
            $productContenInfoFilter::CONDITION_FIELD_NAME_CONTENTINFO,
        ));
        $productContenInfoFilter->where($productContenInfoFilter::CONDITION_FIELD_NAME_TOTAL_DATATYPE,Condition::CONDITION_IN,array(\haibao\erp\model\data\product\ProductContentInfo::DATA_TYPE_IMAGE_PRO,ProductContentInfoData::DATA_TYPE_PRICE));
        $productFilter->leftJoin($productContenInfoFilter,null,ProductFilter::CONDITION_FIELD_NAME_ID,$productContenInfoFilter::CONDITION_FIELD_NAME_PRODUCT_ID,ProductFilter::CONDITION_FIELD_NAME_PRODUCT_CONTENT_INFO);
        $productFilter->order(ProductFilter::CONDITION_FIELD_NAME_ID,false);
        $productFilter->setPager(new Pager($pagesize,1));
        $productList = ProductBusiness::getProduct($productFilter);
        $imageIdArr = array();
        foreach ($productList as $key => $value){
            foreach($value->ProductContentInfo as $info){
                if($info->DataType == ProductContentInfoData::DATA_TYPE_PRICE){
                    $price = json_decode($info->ContentInfo,true);
                    $value->ProductPrice = $price[MisakiCommon::MISAKI_PRODUCT_PRICE_ACCURY];
                }elseif($info->DataType == ProductContentInfoData::DATA_TYPE_IMAGE_PRO){
                    array_push($imageIdArr, $info->ContentInfo);
                    $value->ProductListImage = $info->ContentInfo;
                    break;
                }
            }
        }
        $imageList = ProductBusiness::getInfobyImgeIdArr($imageIdArr);

        foreach ($productList as $k=>$product){
            if(isset($imageList[$product->ProductListImage])){
                $image = $imageList[$product->ProductListImage];
                $product->ProductListImage = \haibao\cms\business\control\Base::getImageLink(substr($image, 1,strlen($image)), 0, 0, 90, 0);
                $product->ProductListImage = str_replace('http://static.localhaibao.cn:81', 'http://static.haibao.cn', $product->ProductListImage);
            }

        }

        return $productList;
    }

    public static function prdocutForPrevAndNext($productId,$CategoryId){

       return ProductBusiness::prdocutForPrevAndNext($productId,$CategoryId);


    }




}